package com.cxmx.escort.common.redis;/**
 * @ClassName:KeyExpiredListener
 * @Description //TODO
 * @Author chenyunxuan
 * @Date $ $
 **/

import com.cxmx.escort.baseEnum.VariableEnum;
import com.cxmx.escort.entity.Bed;
import com.cxmx.escort.service.IBedService;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @ClassName:KeyExpiredListener
 * @Description //TODO
 * @Author chenyunxuan
 * @Date $ $
 **/
@Component
public class KeyExpiredListener extends KeyExpirationEventMessageListener {
    @Autowired
    protected WxPayService wxService;
    @Autowired
    protected IBedService bedService;
    private static final Logger LOGGER = LoggerFactory.getLogger(KeyExpiredListener.class);

    public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
        //过期的key
        String key = new String(message.getBody(), StandardCharsets.UTF_8);
        LOGGER.info("redis key 过期：pattern={},channel={},key={}", new String(pattern), channel, key);
        if (key.startsWith("bed:getUserId:")) {
            String userId = key.split(":")[2];
            String id = String.valueOf(RedisUtils.get("bed:getRefundId:" + userId));
            if (null == id) {
                return;
            }
            LOGGER.info("id={},退还预付款", id);
            WxPayRefundRequest wxPayUnifiedOrderRequest = new WxPayRefundRequest();
            wxPayUnifiedOrderRequest.setRefundFee(VariableEnum.THE_DEPOSIT.getCode());
            wxPayUnifiedOrderRequest.setTotalFee(VariableEnum.THE_DEPOSIT.getCode());
            wxPayUnifiedOrderRequest.setRefundDesc("退还预付款");
            wxPayUnifiedOrderRequest.setOutTradeNo(id);
            wxPayUnifiedOrderRequest.setNotifyUrl("https://api.bochenkj.cn/pay/notify/refund");
            wxPayUnifiedOrderRequest.setRefundFeeType("CNY");
            wxPayUnifiedOrderRequest.setOutRefundNo(id);
            try {
                wxService.refund(wxPayUnifiedOrderRequest);
                RedisUtils.del("bed:getRefundId:" + userId);
            } catch (WxPayException e) {
                e.printStackTrace();
            }
        } else if (key.startsWith("bed:activeStatus:")) {
            String bedId = key.split(":")[2];
            Bed bed = bedService.getById(bedId);
            bed.setFaultFlag(2);
            bedService.updateById(bed);
        }
    }

}